package com.helen.adapter;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.helen.entity.CommitProductEntity;
import com.helen.shopping.R;
import com.helen.ui.account.MoneyListActivity;
import com.helen.ui.account.RefreshMoneyUtils;
import com.helen.utils.ImageLoadUtil;
import com.helen.utils.MyLog;
import com.helen.utils.tiputils.MToast;

import java.util.List;


/**
 * Created by yang on 2018/5/23.
 */

public class CommitOrderListAdapter extends BaseQuickAdapter<CommitProductEntity, BaseViewHolder> {
    private OnSetDaiJinListener onSetDaiJinListener;
    private OnSetJinbaoListener onSetJinbaoListener;
    private int userMaxJinbao;

    public CommitOrderListAdapter(int layoutResId, List<CommitProductEntity> data ) {
        super(layoutResId, data);
        RefreshMoneyUtils refreshMoneyUtils = new RefreshMoneyUtils();
        refreshMoneyUtils.refreshMoneyApi(mContext);
        refreshMoneyUtils.setOnResultListener(new RefreshMoneyUtils.OnResultListener() {
            @Override
            public void onResult(double money,  int jinbao) {
                userMaxJinbao = jinbao;
                MyLog.e("yang","userMaxJinbao=="+userMaxJinbao);
            }
        });
    }

    @Override
    protected void convert(final BaseViewHolder helper, final CommitProductEntity item) {
        try {
            ImageView imgProduct = (ImageView) helper.getView(R.id.img_product);
            TextView tvProductName = (TextView) helper.getView(R.id.tv_product_name);
            TextView tvProductSpec = (TextView) helper.getView(R.id.tv_product_spec);
            TextView tvProductPrice = (TextView) helper.getView(R.id.tv_product_price);
            TextView tvProductCount = (TextView) helper.getView(R.id.tv_product_count);

            LinearLayout llJibao = (LinearLayout) helper.getView(R.id.ll_jinbao);
            View viewJibaoLine = (View) helper.getView(R.id.view_jibao_line);

            LinearLayout llDaijin = (LinearLayout) helper.getView(R.id.ll_daijin);
            View viewDaijinLine = (View) helper.getView(R.id.view_daijin_line);

            TextView tvUserJinbao = (TextView) helper.getView(R.id.tv_user_jinbao);
            final EditText etJibao = (EditText) helper.getView(R.id.et_jibao);
            TextView tvJinbaoLook = (TextView) helper.getView(R.id.tv_jinbao_look);

            final TextView tvDaijin = (TextView) helper.getView(R.id.tv_daijin);

            TextView tvExpressPrice = (TextView) helper.getView(R.id.tv_express_price);

            EditText etNote = (EditText) helper.getView(R.id.et_note);


            if (TextUtils.isEmpty(item.getJinbao() + "") || item.getJinbao() == 0) {
                llJibao.setVisibility(View.GONE);
                viewJibaoLine.setVisibility(View.GONE);
            } else {
                tvUserJinbao.setText("使用金宝(最多使用" + item.getJinbao() + "个): ");
                llJibao.setVisibility(View.VISIBLE);
                viewJibaoLine.setVisibility(View.VISIBLE);

            }

            if (TextUtils.isEmpty(item.getDaijin() + "") || item.getDaijin() == 0) {
                llDaijin.setVisibility(View.GONE);
                viewDaijinLine.setVisibility(View.GONE);
            } else {
                llDaijin.setVisibility(View.VISIBLE);
                viewDaijinLine.setVisibility(View.VISIBLE);
            }

            if (item.getProductImg() != null) {
                ImageLoadUtil.displayImage(mContext, imgProduct, item.getProductImg(), R.mipmap.app_logo, R.mipmap.app_logo);
            }

            tvProductName.setText(item.getProductName());
            tvProductSpec.setText(item.getProductSpec());
            tvProductPrice.setText("￥" + item.getProductPrice());
            tvProductCount.setText("x" + item.getCount());

            tvExpressPrice.setText("￥" + item.getExpressprice());

            tvJinbaoLook.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent ti = new Intent(mContext, MoneyListActivity.class);
                    ti.putExtra("type", 2);
                    mContext.startActivity(ti);
                }
            });

            tvDaijin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onSetDaiJinListener != null) {
                        onSetDaiJinListener.setOnDaiJinListener(helper.getAdapterPosition(), item, tvDaijin);
                    }
                }
            });


            etJibao.setTag(helper.getAdapterPosition());
            etJibao.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!TextUtils.isEmpty(s + "")) {
                        if (etJibao.getTag().equals(helper.getAdapterPosition())) {
                            int mJinBao = Integer.parseInt(s + "");

                            if (mJinBao > item.getJinbao()) {
                                MToast.makeTextLong(mContext, "输入的金宝个数不能大于商品使用的最大金宝个数").show();
                                int selectionStart = etJibao.getSelectionStart();
                                int selectionEnd = etJibao.getSelectionEnd();
                                s.delete(selectionStart - 1, selectionEnd);
                                return;
                            } else {

                                if (etJibao.getTag().equals(helper.getAdapterPosition())) {
                                    item.setmJinbao(mJinBao);
                                }

                                int inputAllJinbao = 0;
                                for (CommitProductEntity commitProductEntity : getData()) {
                                    MyLog.e("yang", "输入的金宝==" + commitProductEntity.getmJinbao());
                                    inputAllJinbao += commitProductEntity.getmJinbao();
                                }

                                if (inputAllJinbao > userMaxJinbao) {
                                    int selectionStart = etJibao.getSelectionStart();
                                    int selectionEnd = etJibao.getSelectionEnd();
                                    s.delete(selectionStart - 1, selectionEnd);
                                    MToast.makeTextLong(mContext, "输入的金宝个数不能大于用户的最大金宝个数").show();
                                    return;
                                }


                            }
                        }

                    } else {
                        if (etJibao.getTag().equals(helper.getAdapterPosition())) {
                            item.setmJinbao(0);
                        }

                    }


                    if (onSetJinbaoListener != null) {
                        onSetJinbaoListener.setOnJinbaoListener(getData());
                    }

                }
            });


            etNote.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    item.setNote(s + "");
                }
            });


        } catch (Exception e) {
            MyLog.e("yang", "设置提交订单列表异常" + e.toString());
        }


    }


    public interface OnSetDaiJinListener {
        void setOnDaiJinListener(int position, CommitProductEntity commitProductEntity, TextView tvDaijin);
    }


    public void setOnItemDaiJinListener(OnSetDaiJinListener onSetDaiJinListener) {
        this.onSetDaiJinListener = onSetDaiJinListener;
    }


    public interface OnSetJinbaoListener {
        void setOnJinbaoListener(List<CommitProductEntity> commitProductEntityList);
    }


    public void setOnItemJinbaoListener(OnSetJinbaoListener onSetJinbaoListener) {
        this.onSetJinbaoListener = onSetJinbaoListener;
    }
}
